package kr.hkit.powerjava.chapter17;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class ImageLabel extends JPanel implements ActionListener {
	private JLabel label;
	private JButton button;

	public ImageLabel() {
		initialize();
	}

	private void initialize() {
		setLayout(new BorderLayout(0, 0));

		label = new JLabel("이미지를 보려면 아래 버튼을 누르세요");
		add(label, BorderLayout.NORTH);
		label.setHorizontalAlignment(SwingConstants.CENTER);

		button = new JButton("이미지 레이블");
		add(button, BorderLayout.SOUTH);
		button.addActionListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == button) {
			actionPerformedButton(e);
		}
	}

	protected void actionPerformedButton(ActionEvent e) {
		ImageIcon icon = new ImageIcon("11.jpg");
		if (label.getIcon() == null) {
			label.setIcon(icon);
			label.setText(null);
		} else {
			label.setIcon(null);
			label.setText("이미지를 보려면 아래 버튼을 누르세요");
		}
	}
}
